package com.itheima.app.messageManager;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.Video;
import com.itheima.service.AnnounCementService;
import com.itheima.service.QuestionService;
import com.itheima.service.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.VideoService;
import com.itheima.vo.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@Component
public class MessageManager {
    @DubboReference
    private UserInfoService userInfoService;
    @DubboReference
    private QuestionService questionService;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private CommentService commentService;
    //回答陌生人问题
    public  void strangerQuestions(Long jiarenId, String reply) {
        Long userId = UserHolder.get().getId();
        UserInfo userInfo = userInfoService.findUserInfoById(userId);
        Question question = questionService.findByUserId(jiarenId);
        if (question==null) {
            question = new Question();
        }
        Map map = new HashMap();
        map.put("userId",userId);
        map.put("huanXinId","HX"+userId);
        map.put("nickname",userInfo.getNickname());
        map.put("strangerQuestion", question.getStrangerQuestion());
        map.put("reply", reply);
        //转jion
        String json = JSON.toJSONString(map);
        //调用环信发送消息
        huanXinTemplate.sendMsg("HX"+jiarenId,json);


    }
    @DubboReference
    private FriendService friendService;
    //添加好友
    public void addContacts(Long friendId) {
        Long userId = UserHolder.get().getId();
        friendService.addContacts(userId,friendId);
        huanXinTemplate.addContacts("HX"+userId,"HX"+friendId);
    }
    //查看联系人
    public ResponseEntity findContactsByPage(Integer pageNum, Integer pageSize) {
        Long userId = UserHolder.get().getId();
        PageBeanVo pageBeanVo = friendService.findContactsByPage(userId, pageNum, pageSize);
        List<ContactVo> contactVos = new ArrayList<>();
        List<Friend> friendList = (List<Friend>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(friendList)) {
            for (Friend friend : friendList) {
                ContactVo contactVo = new ContactVo();
                contactVo.setUserInfo(userInfoService.findUserInfoById(friend.getFriendId()));
                contactVo.setUserId("HX"+friend.getFriendId());
                contactVos.add(contactVo);

            }
        }
        pageBeanVo.setItems(contactVos);
        return ResponseEntity.ok(pageBeanVo);

    }
  //查看喜欢列表
    public ResponseEntity findBycomment(Integer pageNum, Integer pageSize,Integer commentType) {
        Long publishUserId = UserHolder.get().getId();
        //调用rpc获得pbv
        PageBeanVo commentTypeByPage = commentService.findCommentTypeByPage(publishUserId, commentType, pageNum, pageSize);
        //创建封装vo
        ArrayList<UserCommentVo> userCommentVos = new ArrayList<>();
        List<Comment> commentList = (List<Comment>) commentTypeByPage.getItems();
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                UserCommentVo userCommentVo = new UserCommentVo();
                UserInfo userInfo = userInfoService.findUserInfoById(comment.getUserId());
                userCommentVo.setId(userInfo.getId().toString());
                userCommentVo.setAvatar(userInfo.getAvatar());
                userCommentVo.setNickname(userInfo.getNickname());
                userCommentVo.setCreateDate(DateUtil.formatDate(new Date(System.currentTimeMillis())));
                userCommentVos.add(userCommentVo);

            }
        }
        commentTypeByPage.setItems(userCommentVos);
        return ResponseEntity.ok(commentTypeByPage);
    }
    @DubboReference
    private AnnounCementService announCementService;
//公告通知
    public ResponseEntity findAnnouncements(Integer pageNum, Integer pageSize) {
       PageBeanVo pageBeanVo= announCementService.findAll(pageNum,pageSize);
        ArrayList<AnnounCementvo> announCementvos = new ArrayList<>();
        List<Announcement> announCementList = (List<Announcement>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(announCementList)) {
            AnnounCementvo announCementvo = new AnnounCementvo();
            for (Announcement announCement : announCementList) {
                announCementvo.setId(announCement.getId().toString());
                announCementvo.setTitle(announCement.getTitle());
                announCementvo.setDescription(announCement.getDescription());
                announCementvo.setCreatedDate(DateUtil.formatDate(announCement.getCreated()));
                announCementvos.add(announCementvo);
            }
        }
   pageBeanVo.setItems(announCementvos);
        return ResponseEntity.ok(pageBeanVo);
    }
    @Autowired
    private FastFileStorageClient client;

    @Autowired
    private FdfsWebServer webServer;
    @DubboReference
    private VideoService videoService;
    //视频上传
    public void saveVideo(Long userId, MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        // 1.上传封面
        StorePath storePath1 = client.uploadFile(videoThumbnail.getInputStream(), videoThumbnail.getSize(), FileUtil.extName(videoThumbnail.getOriginalFilename()), null);
        String picUrl = webServer.getWebServerUrl() + storePath1.getFullPath();
        // 2.上传视频
        StorePath storePath2 = client.uploadFile(videoFile.getInputStream(), videoFile.getSize(), FileUtil.extName(videoFile.getOriginalFilename()), null);
        String videoUrl = webServer.getWebServerUrl() + storePath2.getFullPath();
        Video video = new Video();
        video.setId(ObjectId.get());
        video.setCreated(System.currentTimeMillis());
        video.setUserId(userId);
        video.setText("巴黎世家");
        video.setPicUrl(picUrl);
        video.setVideoUrl(videoUrl);
        videoService.saveVideo(video);


    }
}
